function resolveData(data) {
    var arr = [];
    for (var k in data) {
        arr.push(k + "=" + data[k])
    }
    return arr.join('&');
}

// console.log(resolveData({ name: 'zs', age: 18 }))

function xhrRequest(option) {
    var xhr = new XMLHttpRequest();

    // 把外界传递过来的参数对象转换为查询字符串
    var str = resolveData(option.data);

    if (option.method.toUpperCase() === 'GET') {
        xhr.open(option.method, option.url + '?' + str);
        xhr.send();
    } else if (option.method.toUpperCase() === 'POST') {
        xhr.open(option.method, option.url);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send(str);
    }
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var data = JSON.parse(xhr.responseText);
            option.success(data);
        }
    }
}